package appfuse.framework.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
 * Access 连接
 * 
 */
public class AccessDB {
	String driver;
	String url;
	String file;
	String user;
	String pwd;	
	Connection conn;
	Statement stmt;
	ResultSet rs;

	public AccessDB() {
		this.driver = "sun.jdbc.odbc.JdbcOdbcDriver";
		this.url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};pwd=1234;";
		this.file = "c:\\windows\\appfuse\\appfuse.mdb";
		
		try {
			Class.forName(driver);
		} catch (final Exception e) {
			
		}
	}
	
	public AccessDB(String bdq) {
		this();
		this.file = "DBQ=" + bdq;
	}
//	创建不可滚动的连接
	public void connect() {
		try {
			String urlStr = url+file;
			conn = DriverManager.getConnection(urlStr,user,pwd);
			stmt = conn.createStatement();
		} catch (Exception e) {
			
		}
	}
//	创建可以滚动的连接
	public void connect2() {
		try {
			String urlStr = url+file;
			conn = DriverManager.getConnection(urlStr,user,pwd);
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		} catch (Exception e) {
		
		}
	}

//	关闭连接
	public void close() {
		try {
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
		
		}
	}

//	查询语句
	public ResultSet executeQuery(String sql) throws SQLException {
		if (stmt == null){
			this.connect();
		}
		rs = stmt.executeQuery(sql);

		return rs;
	}

	/**
	 * ִ执行更新
	 * 
	 * @throws SQLException
	 */
	public void executeUpdate(String sql) throws SQLException {
		if (stmt == null){
			this.connect();
		}
		stmt.executeUpdate(sql);		
	}
} 